<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="企业编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="企业编码"></el-input>
    </el-form-item>
    <el-form-item label="企业名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="企业名称"></el-input>
    </el-form-item>
    <el-form-item label="类型
#{CREATE:创建;REGISTER:注册}" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型
#{CREATE:创建;REGISTER:注册}"></el-input>
    </el-form-item>
    <el-form-item label="状态
#{NORMAL:正常;FORBIDDEN:禁用;WAITING:待审核;REFUSE:拒绝}" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态
#{NORMAL:正常;FORBIDDEN:禁用;WAITING:待审核;REFUSE:拒绝}"></el-input>
    </el-form-item>
    <el-form-item label="是否内置" prop="readonly">
      <el-input v-model="dataForm.readonly" placeholder="是否内置"></el-input>
    </el-form-item>
    <el-form-item label="责任人" prop="duty">
      <el-input v-model="dataForm.duty" placeholder="责任人"></el-input>
    </el-form-item>
    <el-form-item label="有效期
为空表示永久" prop="expirationTime">
      <el-input v-model="dataForm.expirationTime" placeholder="有效期
为空表示永久"></el-input>
    </el-form-item>
    <el-form-item label="logo地址" prop="logo">
      <el-input v-model="dataForm.logo" placeholder="logo地址"></el-input>
    </el-form-item>
    <el-form-item label="企业简介" prop="describe">
      <el-input v-model="dataForm.describe" placeholder="企业简介"></el-input>
    </el-form-item>
    <el-form-item label="用户密码有效期
单位：天 0表示永久有效
" prop="passwordExpire">
      <el-input v-model="dataForm.passwordExpire" placeholder="用户密码有效期
单位：天 0表示永久有效
"></el-input>
    </el-form-item>
    <el-form-item label="是否多地登录" prop="isMultipleLogin">
      <el-input v-model="dataForm.isMultipleLogin" placeholder="是否多地登录"></el-input>
    </el-form-item>
    <el-form-item label="密码输错次数
密码输错锁定账号的次数
单位：次
" prop="passwordErrorNum">
      <el-input v-model="dataForm.passwordErrorNum" placeholder="密码输错次数
密码输错锁定账号的次数
单位：次
"></el-input>
    </el-form-item>
    <el-form-item label="账号锁定时间
密码输错${passwordErrorNum}次后，锁定账号的时间
单位： h | d | w | m
单位： 时 | 天 | 周 | 月
如：0=当天晚上24点 2h = 2小时   2d = 2天  " prop="passwordErrorLockTime">
      <el-input v-model="dataForm.passwordErrorLockTime" placeholder="账号锁定时间
密码输错${passwordErrorNum}次后，锁定账号的时间
单位： h | d | w | m
单位： 时 | 天 | 周 | 月
如：0=当天晚上24点 2h = 2小时   2d = 2天  "></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateUser">
      <el-input v-model="dataForm.updateUser" placeholder="修改人"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          type: '',
          status: '',
          readonly: '',
          duty: '',
          expirationTime: '',
          logo: '',
          describe: '',
          passwordExpire: '',
          isMultipleLogin: '',
          passwordErrorNum: '',
          passwordErrorLockTime: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: ''
        },
        dataRule: {
          code: [
            { required: true, message: '企业编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '企业名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型
#{CREATE:创建;REGISTER:注册}不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态
#{NORMAL:正常;FORBIDDEN:禁用;WAITING:待审核;REFUSE:拒绝}不能为空', trigger: 'blur' }
          ],
          readonly: [
            { required: true, message: '是否内置不能为空', trigger: 'blur' }
          ],
          duty: [
            { required: true, message: '责任人不能为空', trigger: 'blur' }
          ],
          expirationTime: [
            { required: true, message: '有效期
为空表示永久不能为空', trigger: 'blur' }
          ],
          logo: [
            { required: true, message: 'logo地址不能为空', trigger: 'blur' }
          ],
          describe: [
            { required: true, message: '企业简介不能为空', trigger: 'blur' }
          ],
          passwordExpire: [
            { required: true, message: '用户密码有效期
单位：天 0表示永久有效
不能为空', trigger: 'blur' }
          ],
          isMultipleLogin: [
            { required: true, message: '是否多地登录不能为空', trigger: 'blur' }
          ],
          passwordErrorNum: [
            { required: true, message: '密码输错次数
密码输错锁定账号的次数
单位：次
不能为空', trigger: 'blur' }
          ],
          passwordErrorLockTime: [
            { required: true, message: '账号锁定时间
密码输错${passwordErrorNum}次后，锁定账号的时间
单位： h | d | w | m
单位： 时 | 天 | 周 | 月
如：0=当天晚上24点 2h = 2小时   2d = 2天  不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/base/dtenant/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.dTenant.code
                this.dataForm.name = data.dTenant.name
                this.dataForm.type = data.dTenant.type
                this.dataForm.status = data.dTenant.status
                this.dataForm.readonly = data.dTenant.readonly
                this.dataForm.duty = data.dTenant.duty
                this.dataForm.expirationTime = data.dTenant.expirationTime
                this.dataForm.logo = data.dTenant.logo
                this.dataForm.describe = data.dTenant.describe
                this.dataForm.passwordExpire = data.dTenant.passwordExpire
                this.dataForm.isMultipleLogin = data.dTenant.isMultipleLogin
                this.dataForm.passwordErrorNum = data.dTenant.passwordErrorNum
                this.dataForm.passwordErrorLockTime = data.dTenant.passwordErrorLockTime
                this.dataForm.createTime = data.dTenant.createTime
                this.dataForm.createUser = data.dTenant.createUser
                this.dataForm.updateTime = data.dTenant.updateTime
                this.dataForm.updateUser = data.dTenant.updateUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/base/dtenant/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'type': this.dataForm.type,
                'status': this.dataForm.status,
                'readonly': this.dataForm.readonly,
                'duty': this.dataForm.duty,
                'expirationTime': this.dataForm.expirationTime,
                'logo': this.dataForm.logo,
                'describe': this.dataForm.describe,
                'passwordExpire': this.dataForm.passwordExpire,
                'isMultipleLogin': this.dataForm.isMultipleLogin,
                'passwordErrorNum': this.dataForm.passwordErrorNum,
                'passwordErrorLockTime': this.dataForm.passwordErrorLockTime,
                'createTime': this.dataForm.createTime,
                'createUser': this.dataForm.createUser,
                'updateTime': this.dataForm.updateTime,
                'updateUser': this.dataForm.updateUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
